﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
//original https://github.com/bazhenov/naive-bayes-example
namespace NaiveBayes1Test
{
    class Program
    {
        static string nonspamFolderPath = "nonspam-train";
        static string spamFolderPath = "spam-train";
        static string nonspamTestFolderPath = "nonspam-test";
        static string spamTestFolderPath = "spam-test";
        static int correct = 0;
        static int total = 0;
        static void Main(string[] args)
        {
            List<Document> trainCorpus = InitTrainCorpus();

            var c = new Classifier(trainCorpus);

            List<string> spamTestList = InitSpamTests();
            List<string> nonspamTestList = InitNonspamTests();

            foreach(string spamText in spamTestList)
            {
                var rez = c.IsInClassProbability("spam", spamText);

                if (rez < 0.5) correct += 1;
                total += 1;
            }
            foreach (string nonspamText in nonspamTestList)
            {
                var rez = c.IsInClassProbability("nonspam", nonspamText);
                if (rez < 0.5) correct += 1;
                total += 1;
            }

            Console.WriteLine("Correct classified: " + correct);
            Console.WriteLine("Total entries: " + total);
        }

        static List<string> InitSpamTests()
        {
            List<string> spamList = new List<string>();
            foreach (string file in Directory.EnumerateFiles(spamTestFolderPath, "*.txt"))
            {
                
                spamList.Add(File.ReadAllText(file));
            }
            return spamList;
        }

        static List<string> InitNonspamTests()
        {
            List<string> nonspamList = new List<string>();
            foreach (string file in Directory.EnumerateFiles(nonspamTestFolderPath, "*.txt"))
            {
                nonspamList.Add(File.ReadAllText(file));
            }
            return nonspamList;
        }

        static List<Document> InitTrainCorpus(){
            List<Document> trainCorpus = new List<Document>();
            foreach (string file in Directory.EnumerateFiles(nonspamFolderPath, "*.txt"))
            {
                string contents = File.ReadAllText(file);
                
                trainCorpus.Add(new Document("nonspam", contents));
            }
            foreach (string file in Directory.EnumerateFiles(spamFolderPath, "*.txt"))
            {
                string contents = File.ReadAllText(file);
                trainCorpus.Add(new Document("spam", contents));
            }
            return trainCorpus;
        }
    }
}